package com.chy.blog_server.modular.web.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chy.blog_server.core.util.UploadFileUtil;
import com.chy.blog_server.core.util.UploadURLUtil;
import com.chy.blog_server.modular.web.entity.BoPhoto;
import com.chy.blog_server.modular.web.mapper.BoPhotoMapper;
import com.chy.blog_server.modular.web.service.IBoPhotoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 描述：照片表 service业务层
 * 作者：chenhuiyan
 * 时间：2020/02/28 12:03
 */
@Service
@Transactional(readOnly = true)
public class BoPhotoServiceImpl extends ServiceImpl<BoPhotoMapper, BoPhoto> implements IBoPhotoService {

    private final static Logger logger = LoggerFactory.getLogger(BoPhotoServiceImpl.class);

    /**
     * 查询列表
     * @param params
     * @return
     */
    public List<Map<String, Object>> list(Map<String, Object> params){
        return baseMapper.list(params);
    }

    /**
     * 查询总数
     * @param params
     * @return
     */
    public int getCount(Map<String, Object> params){
        return baseMapper.getCount(params);
    }

    /**
     * 根据id查询详情
     * @param id
     * @return
     */
    public Map<String, Object> getInfoById(String id){
        return baseMapper.getInfoById(id);
    }

    /**
     * 查询已删除的照片列表
     * @param params
     * @return
     */
    public List<Map<String, Object>> recycleList(Map<String, Object> params){
        return baseMapper.recycleList(params);
    }

    /**
     * 彻底删除（删除原图和缩略图）
     * @param id
     * @return
     */
    @Transactional(readOnly = false)
    public int thoroughlyDelete(String id){
        //删除照片
        BoPhoto boPhoto = this.getById(id);

        String path = UploadURLUtil.PHOTO_IMG; //路径

        String[] urlArr = new String[2];
        urlArr[0] = path + boPhoto.getUrl();
        urlArr[1] = path + boPhoto.getThumUrl();

        boolean flag = UploadFileUtil.deleteMoreFile(urlArr);

        if(flag){
            logger.info(">>>>>>>>>>>>删除照片成功(" + boPhoto.getUrl() + ")");
        }else{
            logger.info(">>>>>>>>>>>>删除照片失败(" + boPhoto.getUrl() + ")");
        }
        return baseMapper.thoroughlyDelete(id);
    }

    /**
     * 上传照片并生成缩略图
     * @param file
     * @param photoAlbumId
     * @param request
     * @return
     */
    @Transactional(readOnly = false)
    public Map<String, Object> uploadAlbumPhoto(MultipartFile file, String photoAlbumId, HttpServletRequest request){
        Map<String, Object> res = new HashMap<>();
        //上传
        Map<String, Object> map = UploadFileUtil.fileUploadToOne(file, UploadURLUtil.PHOTO_IMG, true);

        BoPhoto boPhoto = new BoPhoto();

        if("200".equals(map.get("state") + "")){
            boPhoto.setUrl(map.get("fileName") + "");
            boPhoto.setImgSize(Integer.valueOf(map.get("fileSize") + ""));
            boPhoto.setThumUrl(map.get("thumFileName") + "");
            boPhoto.setThumSize(Integer.valueOf(map.get("thumFileSize") + ""));
            boPhoto.setPhotoAlbumId(photoAlbumId);
            boPhoto.setType(0);
            boPhoto.preInsert(request);
            this.save(boPhoto);
            res.put("state", "200");
            res.put("message", "成功上传");
        }

        return res;
    }

    /**
     * 上传视频
     * @param file
     * @param photoAlbumId
     * @param request
     * @return
     */
    @Transactional(readOnly = false)
    public Map<String, Object> uploadAlbumPhotoVideo(MultipartFile file, String photoAlbumId, HttpServletRequest request){
        Map<String, Object> res = new HashMap<>();
        //上传
        Map<String, Object> map = UploadFileUtil.fileUploadVideoToOne(file, UploadURLUtil.PHOTO_IMG);

        BoPhoto boPhoto = new BoPhoto();

        if("200".equals(map.get("state") + "")){
            boPhoto.setUrl(map.get("fileName") + "");
            boPhoto.setImgSize(Integer.valueOf(map.get("fileSize") + ""));
            boPhoto.setThumUrl("");
            boPhoto.setThumSize(0);
            boPhoto.setPhotoAlbumId(photoAlbumId);
            boPhoto.setType(1);
            boPhoto.preInsert(request);
            this.save(boPhoto);
            res.put("state", "200");
            res.put("message", "成功上传");
        }

        return res;
    }

}
